import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './route'
import store from "@/store";
import { getUserInfo } from 'libs/util'

Vue.use(VueRouter)

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((to: any, from: any, next: any) => {
    const userInfo: Object = getUserInfo();
    console.log('userInfo', userInfo);
    if (to.path === '/login' || to.path === '/get_number') {
        store.commit('user/setUserInfo', userInfo);
        next()
    } else {
        if (0 in Object.keys(userInfo)) {
            store.commit('user/setUserInfo', userInfo);
            next()
        } else {
            next('/login')
        }
    }

})

export default router
